﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading;
using System.Windows.Forms;

namespace Demo_Multi
{
    public partial class Frm_Multi : Form
    {
        int imgW;
        int imgH;
        byte[][] imgDatas = new byte[4][];
        int[] totals = new int[4];
        ManualResetEvent[] evtShows = { new ManualResetEvent(false), new ManualResetEvent(false), new ManualResetEvent(false), new ManualResetEvent(false) };
        Thread thSig1;
        Thread thSig2;
        Thread thSig3;
        Thread thSig4;
        Thread thShow1;
        Thread thShow2;
        Thread thShow3;
        Thread thShow4;
        Random random = new Random();

        public Frm_Multi()
        {
            InitializeComponent();
            snsPictureBox1.IsUseOpenGL = true;//使用OpenGl进行图像显示
            snsPictureBox1.IsSawtooth = true;
            snsPictureBox1.IsShowThumbnail = false;//不显示缩略图
            snsPictureBox2.IsUseOpenGL = true;//使用OpenGl进行图像显示
            snsPictureBox2.IsSawtooth = true;
            snsPictureBox2.IsShowThumbnail = false;//不显示缩略图
            snsPictureBox3.IsUseOpenGL = true;//使用OpenGl进行图像显示
            snsPictureBox3.IsSawtooth = true;
            snsPictureBox3.IsShowThumbnail = false;//不显示缩略图
            snsPictureBox4.IsUseOpenGL = true;//使用OpenGl进行图像显示
            snsPictureBox4.IsSawtooth = true;
            snsPictureBox4.IsShowThumbnail = false;//不显示缩略图
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //创建图像数据并给图像涂上背景色
            imgW = 1920;
            imgH = 1440;
            for (int i = 0; i < 4; i++)
            {
                imgDatas[i] = new byte[imgW * imgH * 3];
                for (int j = 0; j < imgH; j++)
                {
                    for (int k = 0; k < imgW; k++)
                    {
                        imgDatas[i][(j * imgW + k) * 3] = 10;//b
                        imgDatas[i][(j * imgW + k) * 3 + 1] = 8;//g
                        imgDatas[i][(j * imgW + k) * 3 + 2] = 8;//r
                    }
                }
            }
            thSig1 = new Thread(Signal1);
            thSig1.IsBackground = true;
            thSig1.Start();
            thSig2 = new Thread(Signal2);
            thSig2.IsBackground = true;
            thSig2.Start();
            thSig3 = new Thread(Signal3);
            thSig3.IsBackground = true;
            thSig3.Start();
            thSig4 = new Thread(Signal4);
            thSig4.IsBackground = true;
            thSig4.Start();

            //创建专门用于图像显示的线程
            thShow1 = new Thread(ShowImage1);
            thShow1.IsBackground = true;
            thShow1.Start();
            thShow2 = new Thread(ShowImage2);
            thShow2.IsBackground = true;
            thShow2.Start();
            thShow3 = new Thread(ShowImage3);
            thShow3.IsBackground = true;
            thShow3.Start();
            thShow4 = new Thread(ShowImage4);
            thShow4.IsBackground = true;
            thShow4.Start();
        }

        private void Signal1()
        {
            while (true)
            {
                //记录当前图像的数量
                totals[0]++;
                //给图像显示线程发送信号
                evtShows[0].Set();
                Thread.Sleep(40);
            }
        }

        private void Signal2()
        {
            while (true)
            {
                //记录当前图像的数量
                totals[1]++;
                //给图像显示线程发送信号
                evtShows[1].Set();
                Thread.Sleep(40);
            }
        }

        private void Signal3()
        {
            while (true)
            {
                //记录当前图像的数量
                totals[2]++;
                //给图像显示线程发送信号
                evtShows[2].Set();
                Thread.Sleep(40);
            }
        }

        private void Signal4()
        {
            while (true)
            {
                //记录当前图像的数量
                totals[3]++;
                //给图像显示线程发送信号
                evtShows[3].Set();
                Thread.Sleep(40);
            }
        }

        private void ShowImage1()
        {
            while (true)
            {
                evtShows[0].WaitOne();//等待图像显示信号
                int x = random.Next(10, imgW - 10);
                int y = random.Next(10, imgH - 10);
                int r = random.Next(0, 255);
                int g = random.Next(0, 255);
                int b = random.Next(0, 255);

                imgDatas[0][(y * imgW + x) * 3] = (byte)b;
                imgDatas[0][(y * imgW + x) * 3 + 1] = (byte)g;
                imgDatas[0][(y * imgW + x) * 3 + 2] = (byte)r;

                snsPictureBox1.ShowImage(imgDatas[0], imgW, imgH, PixelFormat.Format24bppRgb);
                evtShows[0].Reset();//复原等待状态
            }
        }

        private void ShowImage2()
        {
            while (true)
            {
                evtShows[1].WaitOne();//等待图像显示信号
                int x = random.Next(10, imgW - 10);
                int y = random.Next(10, imgH - 10);
                int r = random.Next(0, 255);
                int g = random.Next(0, 255);
                int b = random.Next(0, 255);

                imgDatas[1][(y * imgW + x) * 3] = (byte)b;
                imgDatas[1][(y * imgW + x) * 3 + 1] = (byte)g;
                imgDatas[1][(y * imgW + x) * 3 + 2] = (byte)r;

                snsPictureBox2.ShowImage(imgDatas[1], imgW, imgH, PixelFormat.Format24bppRgb);
                evtShows[1].Reset();//复原等待状态
            }
        }

        private void ShowImage3()
        {
            while (true)
            {
                evtShows[2].WaitOne();//等待图像显示信号
                int x = random.Next(10, imgW - 10);
                int y = random.Next(10, imgH - 10);
                int r = random.Next(0, 255);
                int g = random.Next(0, 255);
                int b = random.Next(0, 255);

                imgDatas[2][(y * imgW + x) * 3] = (byte)b;
                imgDatas[2][(y * imgW + x) * 3 + 1] = (byte)g;
                imgDatas[2][(y * imgW + x) * 3 + 2] = (byte)r;

                snsPictureBox3.ShowImage(imgDatas[2], imgW, imgH, PixelFormat.Format24bppRgb);
                evtShows[2].Reset();//复原等待状态
            }
        }

        private void ShowImage4()
        {
            while (true)
            {
                evtShows[3].WaitOne();//等待图像显示信号
                int x = random.Next(10, imgW - 10);
                int y = random.Next(10, imgH - 10);
                int r = random.Next(0, 255);
                int g = random.Next(0, 255);
                int b = random.Next(0, 255);

                imgDatas[3][(y * imgW + x) * 3] = (byte)b;
                imgDatas[3][(y * imgW + x) * 3 + 1] = (byte)g;
                imgDatas[3][(y * imgW + x) * 3 + 2] = (byte)r;

                snsPictureBox4.ShowImage(imgDatas[3], imgW, imgH, PixelFormat.Format24bppRgb);
                evtShows[3].Reset();//复原等待状态
            }
        }

        private void snsPictureBox1_Paint(object sender, PaintEventArgs e)
        {
            string strTime = "";
            strTime += string.Format("Size:{0}x{1}", snsPictureBox1.ImgWidth, snsPictureBox1.ImgHeight);
            strTime += string.Format("     Scale:{0:F2}", snsPictureBox1.SnsScale);
            strTime += string.Format("     ShowFps:{0:F1}", snsPictureBox1.ShowFps);
            strTime += string.Format("     ImgId:{0}", totals[0]);
            snsPictureBox1.ShowString(strTime, new PointF(snsPictureBox1.XSpace + 1, snsPictureBox1.YSpace + 1), false);
        }

        private void snsPictureBox2_Paint(object sender, PaintEventArgs e)
        {
            string strTime = "";
            strTime += string.Format("Size:{0}x{1}", snsPictureBox2.ImgWidth, snsPictureBox2.ImgHeight);
            strTime += string.Format("     Scale:{0:F2}", snsPictureBox2.SnsScale);
            strTime += string.Format("     ShowFps:{0:F1}", snsPictureBox2.ShowFps);
            strTime += string.Format("     ImgId:{0}", totals[1]);
            snsPictureBox1.ShowString(strTime, new PointF(snsPictureBox1.XSpace + 1, snsPictureBox1.YSpace + 1), false);
        }

        private void snsPictureBox3_Paint(object sender, PaintEventArgs e)
        {
            string strTime = "";
            strTime += string.Format("Size:{0}x{1}", snsPictureBox3.ImgWidth, snsPictureBox3.ImgHeight);
            strTime += string.Format("     Scale:{0:F2}", snsPictureBox3.SnsScale);
            strTime += string.Format("     ShowFps:{0:F1}", snsPictureBox3.ShowFps);
            strTime += string.Format("     ImgId:{0}", totals[2]);
            snsPictureBox1.ShowString(strTime, new PointF(snsPictureBox1.XSpace + 1, snsPictureBox1.YSpace + 1), false);
        }

        private void snsPictureBox4_Paint(object sender, PaintEventArgs e)
        {
            string strTime = "";
            strTime += string.Format("Size:{0}x{1}", snsPictureBox4.ImgWidth, snsPictureBox4.ImgHeight);
            strTime += string.Format("     Scale:{0:F2}", snsPictureBox4.SnsScale);
            strTime += string.Format("     ShowFps:{0:F1}", snsPictureBox4.ShowFps);
            strTime += string.Format("     ImgId:{0}", totals[3]);
            snsPictureBox1.ShowString(strTime, new PointF(snsPictureBox1.XSpace + 1, snsPictureBox1.YSpace + 1), false);
        }

        private void snsPictureBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            snsPictureBox1.Init();
        }

        private void snsPictureBox2_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            snsPictureBox2.Init();
        }

        private void snsPictureBox3_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            snsPictureBox3.Init();
        }

        private void snsPictureBox4_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            snsPictureBox4.Init();
        }

        
    }
}
